#include "CameraControl.hpp"
#include <sstream>

CameraControlException::CameraControlException(std::string msg)
{
  message=std::string()+"NetworkError : "+msg;
}

const char* CameraControlException::what() const noexcept
{
  return message.c_str();
}

CameraControl::CameraControl(std::string CameraIP) : webClient(std::string()+"http://"+CameraIP)
{
}

CameraControl::~CameraControl()
{
}

void CameraControl::OpenSession()
{
  auto res=webClient.Get("/ctrl/session");
  nlohmann::json jsonData=nlohmann::json::parse(res->body);
  if(jsonData["code"]!=0)
  {
    throw CameraControlException("Can't Open Session");
  }
}

nlohmann::json CameraControl::GetCameraInfo()
{
  auto res=webClient.Get("/info");
  if(res==nullptr)
    throw CameraControlException("Can't send HTTP request");
  else if(res->status==200)
    return nlohmann::json::parse(res->body);
  else
    throw CameraControlException((std::stringstream()<<"The request is error code : "<<res->status).str());
}

void CameraControl::RecordMode()
{
  auto res=webClient.Get("/ctrl/mode?action=to_rec");
  nlohmann::json jsonData=nlohmann::json::parse(res->body);
  if(jsonData["code"]!=0)
    throw CameraControlException("Can't Switch Mode to Record");
}

CameraControl::Mode CameraControl::GetMode()
{
  auto res=webClient.Get("/ctrl/mode?action=query");
  nlohmann::json jsonData=nlohmann::json::parse(res->body);
  if(jsonData["code"]!=0)
    throw CameraControlException("Can't Get Mode");
  Mode mode;
  if(jsonData["msg"]=="pb")
    mode=Mode::Play;
  else if(jsonData["msg"]=="rec")
    mode=Mode::Record;
  else if(jsonData["msg"]=="standby")
    mode=Mode::Ready;
  else
    throw CameraControlException(std::string()+"Unknow Mode"+jsonData["msg"].get<std::string>());
  return mode;
}

void CameraControl::SetStreamSource(Stream stream)
{
  std::string GetString;
  switch(stream)
  {
    case Stream::Stream0:
      GetString="/ctrl/set?send_stream=Stream0";
    break;
    case Stream::Stream1:
      GetString="/ctrl/set?send_stream=Stream1";
    break;
  }
  auto res=webClient.Get(GetString);
  if(res==nullptr)
  {
    throw CameraControlException("Can't connect to camera");
  }
  nlohmann::json jsonData=nlohmann::json::parse(res->body);
  if(jsonData["code"]!=0)
    throw CameraControlException("Can't Switch Stream Source");
}

nlohmann::json CameraControl::GetStreamPara(Stream stream)
{
  std::string GetString;
  switch(stream)
  {
    case Stream::Stream0:
      break;
    case Stream::Stream1:
      break;
  }
  auto res=webClient.Get(GetString);
  return nlohmann::json::parse(res->body);
}

std::string CameraControl::GetSerialNumber()
{
  auto res=GetCameraInfo();
  if(res["sn"].is_string())
    return res["sn"].get<std::string>();
  else
    throw CameraControlException("UUID format is error");
}

void CameraControl::StartRecord()
{
  auto res=webClient.Get("/ctrl/rec?action=start");
  nlohmann::json jsonData=nlohmann::json::parse(res->body);
  if(jsonData["code"]!=0)
    throw CameraControlException("Can't control camera to shot");
}

void CameraControl::StopRecord()
{
  auto res=webClient.Get("/ctrl/rec?action=stop");
  nlohmann::json jsonData=nlohmann::json::parse(res->body);
  if(jsonData["code"]!=0)
    throw CameraControlException("Can't control camera stop shot");
}


